﻿using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.ViewModels;

namespace UnitTest.NotTestViewModel
{
    public class TestFavoritePageViewModel
    {
        [Test]
        public async Task TestPageAppearingCommand()
        {
            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var favorite = new Favorite
            {
                IsFavorite = true,
                StoreUid = "123",
                Timestamp = 1234354
            };
            var list = new List<Favorite>();
            list.Add(favorite);
            favoriteStorageMock.Setup(p => p.GetFavoritesAsync())
                .ReturnsAsync(list);
            var mockFavoriteStorage = favoriteStorageMock.Object;

            var storeStorageMock = new Mock<IStoreStorage>();
            storeStorageMock
                .Setup(p => p.GetStoreByStoreUidAsync(It.IsAny<string>()))
                .ReturnsAsync((Store)null);
            var mockStoreStorage = storeStorageMock.Object;
            var favoritePageViewModel =
                new FavoritePageViewModel(mockFavoriteStorage, mockStoreStorage,
                    null);
            await favoritePageViewModel.PageAppearingCommandFunction();

            favoriteStorageMock.Verify(p => p.GetFavoritesAsync(), Times.Once);
            storeStorageMock.Verify(
                p => p.GetStoreByStoreUidAsync(It.IsAny<string>()),
                Times.AtLeastOnce);

            Assert.IsFalse(favoritePageViewModel.Loading);
        }

        [Test]
        public async Task TestStoreTappedCommand()
        {
            var dateTime = new DateTime();
            var storeFavorite = new StoreFavorite(null, null, dateTime);
            var punchNavigationServiceMock =
                new Mock<IPunchNavigationService>();
            var mockPunchNavigationService = punchNavigationServiceMock.Object;
            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var mockFavoriteStorage = favoriteStorageMock.Object;
            var favoritePageViewModel =
                new FavoritePageViewModel(mockFavoriteStorage, null,
                    mockPunchNavigationService);
            await favoritePageViewModel.StoreTappedCommandFunction(
                storeFavorite);

            punchNavigationServiceMock.Verify(
                p => p.NavigateToAsync(
                    ContentNavigationConstant.StorePunchesPage,
                    storeFavorite.Store), Times.Once);
        }

        [Test]
        public async Task TestFavoriteStorageOnUpdatedIsFavorite()
        {
            var now = DateTime.Now.Ticks;
            var dateTime = new DateTime();
            var favorite = new Favorite { Timestamp = now, IsFavorite = true, StoreUid = "123" };
            var store = new Store { Uid = "123" };

            //函数调用参数准备
            var e = new FavoriteStorageUpdatedEventArgs(
                favorite);
            var sender = new object();

            var storeStorageMock = new Mock<IStoreStorage>();
            storeStorageMock.Setup(p => p.GetStoreByStoreUidAsync("123"))
                .ReturnsAsync(store);

            var mockStoreStorage = storeStorageMock.Object;

            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var mockFavoriteStorage = favoriteStorageMock.Object;
            var favoritePageViewModel =
                new FavoritePageViewModel(mockFavoriteStorage, mockStoreStorage, null);
            favoritePageViewModel.StoreFavoriteCollection.Add(new StoreFavorite(store, favorite, dateTime));
            favoritePageViewModel.FavoriteStorageOnUpdated(sender, e);
            storeStorageMock.Verify(p => p.GetStoreByStoreUidAsync("123"),
                Times.AtLeastOnce);

        }

        [Test]
        public async Task TestFavoriteStorageOnUpdatedNotFavorite()
        {
            var now = DateTime.Now.Ticks;
            var dateTime = new DateTime();
            var favorite = new Favorite { Timestamp = now, IsFavorite = false, StoreUid = "123" };
            var store = new Store { Uid = "123" };

            //函数调用参数准备
            var e = new FavoriteStorageUpdatedEventArgs(
                favorite);
            var sender = new object();

            var storeStorageMock = new Mock<IStoreStorage>();
            storeStorageMock.Setup(p => p.GetStoreByStoreUidAsync("123"))
                .ReturnsAsync(store);

            var mockStoreStorage = storeStorageMock.Object;

            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var mockFavoriteStorage = favoriteStorageMock.Object;
            var favoritePageViewModel =
                new FavoritePageViewModel(mockFavoriteStorage, mockStoreStorage, null);
            favoritePageViewModel.StoreFavoriteCollection.Add(new StoreFavorite(store, favorite, dateTime));
            favoritePageViewModel.FavoriteStorageOnUpdated(sender, e);
            storeStorageMock.Verify(p => p.GetStoreByStoreUidAsync("123"),
                Times.Never);

        }
    }
}
